package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsCertificate;
import yx.model.BsPaper;
import yx.model.BsStudent;
import yx.model.SmCode;
import yx.vo.DatagridVO;
import yx.vo.CertificateVO;
import yx.vo.PaperVO;

@Service("certificateService")
public class CertificateServiceImpl implements CertificateServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CertificateServiceImpl.class);

	private BaseDaoInt<BsCertificate> certificateDao;
	private BaseDaoInt<BsStudent> studentDao;
	private BaseDaoInt<SmCode> codeDao;

	public BaseDaoInt<BsCertificate> getCertificateDao() {
		return certificateDao;
	}

	@Autowired
	public void setCertificateDao(BaseDaoInt<BsCertificate> certificateDao) {
		this.certificateDao = certificateDao;
	}

	public BaseDaoInt<BsStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoInt<BsStudent> studentDao) {
		this.studentDao = studentDao;
	}

	public BaseDaoInt<SmCode> getCodeDao() {
		return codeDao;
	}

	@Autowired
	public void setCodeDao(BaseDaoInt<SmCode> codeDao) {
		this.codeDao = codeDao;
	}

	@Override
	public CertificateVO save(CertificateVO certificate) {
		BsCertificate t = new BsCertificate();
		BeanUtils.copyProperties(certificate, t);
		certificateDao.save(t);
		BeanUtils.copyProperties(t, certificate);

		// 姓名和学号
		BsStudent stu = studentDao.get(BsStudent.class, certificate.getStudentId());
		certificate.setStudentName(stu.getStudentName());
		certificate.setStudentNo(stu.getStudentNo());
		// 是否毕业
		if (certificate.getFinishFlag() != null && !"".equals(certificate.getFinishFlag())) {
			Map<String, String> retMap = getDrawMethodAndFinishFlag("04");
			certificate.setFinishFlagName(retMap.get(certificate.getFinishFlag()));
		}
		// 领取方式
		if (certificate.getDrawMethod() != null && !"".equals(certificate.getDrawMethod())) {
			Map<String, String> retMap = getDrawMethodAndFinishFlag("03");
			certificate.setDrawMethodName(retMap.get(certificate.getDrawMethod()));
		}
		return certificate;
	}

	@Override
	public DatagridVO datagrid(CertificateVO certificate) {
		DatagridVO dg = new DatagridVO();
		String hql = " from BsCertificate c, BsStudent s where s.id = c.studentId";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(certificate, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(certificate, hql);
		List l = certificateDao.find(hql, params, certificate.getPage(), certificate.getRows());
		List<CertificateVO> nl = new ArrayList<CertificateVO>();
		changeModel(l, nl);
		dg.setTotal(certificateDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List l, List<CertificateVO> nl) {
		if (l != null && l.size() > 0) {
			Map<String, String> retMap = getDrawMethodAndFinishFlag("04");
			Map<String, String> retMap2 = getDrawMethodAndFinishFlag("03");
			for (int i = 0; i < l.size(); i++) {
				Object[] obj = (Object[]) l.get(i);
				BsCertificate c = (BsCertificate) obj[0];
				BsStudent s = (BsStudent) obj[1];

				CertificateVO vo = new CertificateVO();
				BeanUtils.copyProperties(c, vo);
				vo.setStudentId(s.getId());
				vo.setStudentName(s.getStudentName());
				vo.setStudentNo(s.getStudentNo());
				// 是否毕业
				if (vo.getFinishFlag() != null && !"".equals(vo.getFinishFlag())) {
					vo.setFinishFlagName(retMap.get(vo.getFinishFlag()));
				}
				// 领取方式
				if (vo.getDrawMethod() != null && !"".equals(vo.getDrawMethod())) {
					vo.setDrawMethodName(retMap2.get(vo.getDrawMethod()));
				}
				nl.add(vo);
			}
		}
	}

	private String addOrder(CertificateVO certificate, String hql) {
		if (certificate.getSort() != null || certificate.getOrder() != null) {
			hql += " order by " + certificate.getSort() + " " + certificate.getOrder();
		}
		return hql;
	}

	private String addWhere(CertificateVO certificate, String hql, Map<String, Object> params) {
		if (certificate.getStudentName() != null && !"".equals(certificate.getStudentName().trim())) {
			hql += " and s.studentName like :studentName";
			params.put("studentName", "%%" + certificate.getStudentName().trim() + "%%");
		}
		if (certificate.getStudentNo() != null && !"".equals(certificate.getStudentNo().trim())) {
			hql += " and s.studentNo = :studentNo";
			params.put("studentNo", certificate.getStudentNo().trim());
		}
		return hql;
	}
	
	// 获得企业类型
	private Map<String, String> getDrawMethodAndFinishFlag(String type) {
		Map<String, String> retMap = new HashMap<String, String>();
		if (type != null && !"".equals(type)) {
			String hql = "from SmCode t where t.ctype = :ctype";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("ctype", type);
			List<SmCode> l = codeDao.find(hql, params);
			for (SmCode c : l) {
				retMap.put(c.getCvalue(), c.getCname());
			}
		}
		return retMap;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsCertificate t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		certificateDao.executeHql(hql);
	}

	@Override
	public CertificateVO edit(CertificateVO certificate) {
		BsCertificate t = certificateDao.get(BsCertificate.class, certificate.getId());
		BeanUtils.copyProperties(certificate, t);
		// 是否毕业
		if (certificate.getFinishFlag() != null && !"".equals(certificate.getFinishFlag())) {
			Map<String, String> retMap = getDrawMethodAndFinishFlag("04");
			certificate.setFinishFlagName(retMap.get(certificate.getFinishFlag()));
		}
		// 领取方式
		if (certificate.getDrawMethod() != null && !"".equals(certificate.getDrawMethod())) {
			Map<String, String> retMap = getDrawMethodAndFinishFlag("03");
			certificate.setDrawMethodName(retMap.get(certificate.getDrawMethod()));
		}
		return certificate;
	}

}
